package com.feifan.provider.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.feifan.api.model.UmsLogTypeExample;
import com.feifan.api.model.UmsUserView;
import com.feifan.api.service.IUmsUserViewService;
import com.feifan.common.CommonPage;
import com.feifan.provider.mapper.UmsLogTypeMapper;
import com.feifan.provider.mapper.redis.mapper.RedisUtilMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import java.text.SimpleDateFormat;
import java.util.*;

import static java.util.Calendar.DAY_OF_MONTH;

/**
 * @program: FinalProject
 * @author: Xiaofan
 * @createTime: 2021-08-13 09:35
 * @description: Functions of this class is
 **/
@Service(
        version = "1.0.0",
        interfaceName = "com.feifan.api.service.IUmsUserViewService",
        interfaceClass = IUmsUserViewService.class
)
@Transactional
public class UmsUserViewService implements IUmsUserViewService {

    @Autowired
    private UmsLogTypeMapper dao;

    @Autowired
    private RedisUtilMapper redisdao;

    @Override
    public CommonPage listUV(String start, String end, String type) {
        List uvList=forDate(start,end,type);

        return CommonPage.restPage(uvList);
    }

    //根据开始、结束时间获取集合，循环每一个从redis中得到数据
    private List<UmsUserView> forDate(String start, String end, String type) {
        //在使用SimpleDateFormat时格式化时间的 yyyy.MM.dd 为年月日而如果希望格式化时间为12小时制的，则使用hh:mm:ss 如果希望格式化时间为24小时制的，则使用HH:mm:ss
        //妈的一开始用的是mm，搞了很久操******
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//用于限定转换成日期的格式
        List<UmsUserView> uvList = new ArrayList<>();
        try {
            Date d1 = sdf.parse(start);//把一个字符串转换成日期
            Date d2 = sdf.parse(end);//把一个字符串转换成日期
            //开始遍历d1到d2这些天的数据
            Date temp = d1;
            Calendar calendar = Calendar.getInstance();//设置一个临时变量操作每天天数的相加
            calendar.setTime(d1);
            while (temp.getTime() <= d2.getTime()) {
//                System.out.println(sdf.format(temp));
                temp = calendar.getTime();
                Set<Object> set = redisdao.getAllkeys(sdf.format(temp) + "_" + type);//按照所规定的格式来把所有的键给取出放到集合中

                set.forEach(c -> {
                    //取出每种日志类型的个数
                    int count = Integer.parseInt(redisdao.get(c.toString()).toString());
                    uvList.add(new UmsUserView(c.toString().substring(0,10),count));//只取出时间中的日期，年月日即可
                });
                calendar.add(DAY_OF_MONTH, 1);//就相当与将每天的日期每次加一，这样来一次循环
            }

            System.out.println(uvList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return uvList;
    }

    @Override
    public CommonPage listTypeUV() {
        UmsLogTypeExample example = new UmsLogTypeExample();
        example.createCriteria().andFlagEqualTo(1);//找出1的类型
        return CommonPage.restPage(dao.selectByExample(example));
    }
}